package com.basejace.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.basejace.common.model.PubControl;
import com.basejace.common.model.PubDictdata;
import com.basejace.common.model.PubUser;
import com.basejace.core.R;
import com.basejace.core.base.BaseController;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;
import com.jfinal.plugin.activerecord.tx.Tx;

public class ControlController extends BaseController {
	protected Log log = Log.getLog(this.getClass());

	public ControlController() {
		//如果需要设置特别的model，需要在这个构造函数里面使用
		this.setModelClass(PubControl.class);
	}
	
	public void tree() {
		List<PubControl> controls = PubControl.dao.find("select * from pub_control order by priority desc");
		JSONArray ao = new JSONArray();
		for (int i = 0; i < controls.size(); i++) {
			JSONObject obj = new JSONObject();
			obj.put("id", controls.get(i).getControlid());
			obj.put("parent", controls.get(i).getControlpid());
			obj.put("text", controls.get(i).getCname());
			
			/*if (controls.get(i).getControlid().equals("00")) {
				Map<String,Object> openstate = new HashMap<String,Object>();
				openstate.put("opened", true);
				obj.put("state", new JSONObject(openstate));
			}*/
			
			if ("0".equals(controls.get(i).getControlType())) {
				obj.put("icon", "fa fa-folder icon-state-warning icon-lg");
			} else if ("1".equals(controls.get(i).getControlType())) {
				obj.put("icon", "fa fa-file icon-state-warning icon-lg");
			} else if ("2".equals(controls.get(i).getControlType())) {
				obj.put("icon", "fa fa-cogs icon-lg");
			}
			ao.add(obj);
		}
		
		renderJson(ao);
	}
	
	public void beforeAdd() {
		//设置主键和父ID
		String controlid = getPara("controlid");
		String currentMaxId = Db.queryStr("select max(controlid) from pub_control where controlpid = '" + controlid + "'");
		String newId = "";
		if (currentMaxId == null) {
			newId = controlid + "01";
		} else {
			String end2num = StringUtils.substringAfter(currentMaxId, controlid);
			if (Integer.parseInt(end2num) < 9){
				end2num = "0" + (Integer.parseInt(end2num)+1);
			} else {
				end2num = "" + (Integer.parseInt(end2num)+1);
			}
			newId = controlid + end2num;
		}
		
		PubControl pubControl = new PubControl();
		pubControl.set("controlid", newId);
		pubControl.set("controlpid", controlid);
		setAttr("pubControl", pubControl);
	}
	
	@Before(Tx.class)
	public void delete() {
		
		try {
			Object[] primaryKeys = getPK(getPara(R.Key.PK)); //可能有多组主键
			for (int i=0; i<primaryKeys.length; i++) {
				this.getModelEntity().deleteById(primaryKeys[i]);
//				或者使用下面这句
//				Db.deleteById(getTableName(getModelClass()), getTablePrimaryKey(getModelClass()), primaryKeys[i]);
			}
			this.afterDelete();
		} catch (Exception e) {
			e.printStackTrace();
			setAttr("message", "删除失败：" + e.getMessage());
			
		}
		render(CONTENT);
	}
	public void afterSave() throws Exception  {
		setAttr("upateParent", "true");
	}
	public void afterDelete()throws Exception  {
		setAttr("upateParent", "true");
	}
	

}
